#import "ViewListener.h"
#import "drawingEvent.h"

LineListener::LineListener(View* v) : DrawingListener(LINE)
{
	vista = v;
}

void LineListener::action(void* receiver, Event* data)
{
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
	
	///// Trabajo con el evento /////////
	
	LineEvent* evt = (LineEvent*)data;
	
	NSPoint p0 = (NSPoint){evt->x0(), evt->y0()};
	NSPoint p1 = (NSPoint){evt->x1(), evt->y1()};
	NSString* str_color = [NSString stringWithCString:evt->color()];
	
	//////////////////////////////////////
	
	[vista drawLineFromPoint:p0 toPoint:p1 withColor: [NSColor colorFromHexRGB:str_color]];
	
	[pool release];
}	

CircleListener::CircleListener(View* v) : DrawingListener(CIRCLE)
{
	vista = v;
}

void CircleListener::action(void* receiver, Event* data)
{	
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
	
	///// Trabajo con el evento /////////
	
	CircleEvent* evt = (CircleEvent*)data;
	NSPoint p = NSMakePoint(evt->x(), evt->y());
	NSString* str_color = [NSString stringWithCString:evt->color()];
	
	//////////////////////////////////////
	
	[vista drawOvalFromPoint:p withRadius:evt->radius() withColor:[NSColor colorFromHexRGB:str_color]];
	
	[pool release];
}

RectangleListener::RectangleListener(View* v) : DrawingListener(RECTANGLE)
{
	vista = v;
}

void RectangleListener::action(void* receiver, Event* data)
{		
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
	
	NSPoint p;
	NSSize size;
	
	///// Trabajo con el evento /////////
	
	RectangleEvent* evt = (RectangleEvent*)data;
	p = (NSPoint){evt->x(), evt->y()};
	size = (NSSize){evt->width(), evt->height()};
	NSString* str_color = [NSString stringWithCString:evt->color()];
	
	//////////////////////////////////////
	
	[vista drawRectFromPoint:p withSize:size withColor:[NSColor colorFromHexRGB:str_color]];
	
	[pool release];
}

HandDrawListener::HandDrawListener(View* v) : DrawingListener(HAND_DRAW)
{
	vista = v;
}

void HandDrawListener::action(void* receiver, Event* data)
{	
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
	
	///// Trabajo con el evento /////////
	
	HandDrawEvent* evt = (HandDrawEvent*)data;
	NSPoint p;
	int n = evt->npoints();
	NSMutableArray* array = [[NSMutableArray alloc] init];
	
	for (int i=0; i<n; i++)
	{
		p = (NSPoint){evt->xn(i), evt->yn(i)};
		[array addObject:[NSValue valueWithPoint:p]];
		
		if (i+1 < n)
		{
			p = (NSPoint){evt->xn(i+1), evt->yn(i+1)};
			[array addObject:[NSValue valueWithPoint:p]];
		}
	}
	
	//////////////////////////////////////
	
	NSString* str_color = [NSString stringWithCString:evt->color()];
	
	[vista drawPoints:array withColor:[NSColor colorFromHexRGB:str_color]];
	
	[pool release];
}

MethodListener::MethodListener(View* v)
{
	vista = v;
}

bool MethodListener::check_event(Event* evt)
{
	if ((evt->get_type() == METHOD_LISTENER_TYPE) ||
		(evt->get_type() == PAIR_EVENT_TYPE))
	{
		return true;
	}
	else return false;
}

void MethodListener::action(void* receiver, Event* ev)
{
	const char* method;
	GenericObject obj, w, h;
	PortableFloat width,height;
	
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
	
	if (ev->get_type() == METHOD_LISTENER_TYPE)
	{
		obj = ev->member_value("method");
		obj >>= method;
	
		if (strcmp(method, "clean_all")==0)
		{
			[vista cleanAll];
		}
		
		else if (strcmp(method, "resize") == 0)
		{
			w = ev->member_value("width");
			w >>= width;
			
			h = ev->member_value("height");
			h >>= height;
			
			NSSize size = NSMakeSize((float)width, (float)height);
			[vista setFrameSize:size];
			NSRect rect = [[vista window] frame];
			rect.size = size;
			
			[[vista window] setFrame:rect display:YES];
			
			[vista setNeedsDisplay:YES];
		}
	}
	
	if (ev->get_type() == PAIR_EVENT_TYPE)
	{
		PairEvent* pair = (PairEvent*)ev;
		float f0 =pair->get_x();
		float f1 =pair->get_y();
		
		[vista showPanel:[NSString stringWithFormat:@"(%d, %d)",(int)f0,(int)f1]];
		NSRect rect = [[vista window] frame];
		rect.origin = NSMakePoint(f0, f1);
		[[vista window] setFrame:rect display:YES];
	}
	
	[pool release];
}

const char* MethodListener::name()
{
	return "MethodListener";
}

TempListener::TempListener(View* v)
{
	vista = v;
}

bool TempListener::check_event(Event* evt)
{
	if (evt->get_type() == 5678)
		return true;
	else return false;
}

void TempListener::action(void* receiver, Event* ev)
{	
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
	
	PortableFloat f;
	GenericObject obj = ev->member_value("value");
	obj >>= f;
	
	[vista setTemp:(float)f];
	
	[pool release];
}

const char* TempListener::name()
{
	return "TempListener";
}


